package com.jye.cloud.user.controller;



import com.jye.cloud.domain.JsonResult;
import com.jye.cloud.domain.Record;
import com.jye.cloud.user.service.GroupService;
import com.jye.cloud.user.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/record")
@Api(tags="记录控制器API")
public class RecordController {

    @Autowired
    RecordService recordService;

    @Autowired
    GroupService groupService;

    @GetMapping("/findRecordById")
    @ApiOperation(value="查找记录", notes="根据id查找记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "记录ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findRecordById(int id){
        JsonResult jsonResult = new JsonResult();
        Record record = recordService.findRecordById(id);
        if(!StringUtils.isEmpty(record)){
            record.setGroup(groupService.findGroupById(record.getPId()));
            jsonResult.setData(record);
        }else{
            jsonResult.setMsg("并无记录!请重试!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findMyRecordByUId")
    @ApiOperation(value="查找我的记录", notes="根据uId查找我的记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "skip",
                    value = "跳过几条", dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "limit",
                    value = "限制几条", dataType = "Integer"),
    })
    public JsonResult findMyRecordByUId(int uId, @RequestParam(required = false) Integer skip, @RequestParam(required = false)Integer limit){
        JsonResult jsonResult = new JsonResult();
        List<Record> recordList;
        if(skip == null && limit == null){
            recordList = recordService.findMyRecordByUId(uId);
        }else{
            recordList = recordService.findMyRecordByUIdLimit(uId,skip,limit);
        }

        if(recordList.size()!=0){
            jsonResult.setData(recordList);
        }else {
            jsonResult.setMsg("你并无记录!请重试!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @PostMapping("/addRecord")
    @ApiOperation(value="添加记录", notes="根据questions,uId,pId,records,answers添加记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "record",
                    value = "Record实体类", required = true),
    })
    public JsonResult addRecord(Record record){
        Integer i = recordService.isExit(record.getUId(),record.getPId());
        if(i != null){
            return new JsonResult("500","已作答!请勿重新作答!",i);
        }
        int id = recordService.addRecord(record);
        if(id != 0){
            return new JsonResult(null,"添加成功",id);
        }else{
            return new JsonResult("500","添加失败",0);
        }
    }

}
